﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Media;

namespace Xceed.Silverlight.DataGrid
{
  #region Metadata

  [StyleTypedProperty( Property = "CellStyle", StyleTargetType = typeof( DataCell ) )]

  [TemplateVisualState( GroupName = "CommonStates", Name = "Alternate" )]
  [TemplateVisualState( GroupName = "CommonStates", Name = "AlternateMouseOver" )]

  [TemplateVisualState( GroupName = "InitializationStates", Name = "Initialized" )]
  [TemplateVisualState( GroupName = "InitializationStates", Name = "NotInitialized" )]

  #endregion

  public class DataRow : EditableRow, IDataGridElement
  {
    internal static readonly string CommonStateMouseOverSelected = "MouseOverSelected";
    internal static readonly string CommonStateAlternateMouseOver = "AlternateMouseOver";
    internal static readonly string CommonStateMouseOver = "MouseOver";
    internal static readonly string CommonStateSelected = "Selected";
    internal static readonly string CommonStateAlternate = "Alternate";
    internal static readonly string CommonStateNormal = "Normal";
    internal static readonly string InitializationStateInitialized = "Initialized";
    internal static readonly string InitializationStateNotInitialized = "NotInitialized";

    public DataRow()
      : base()
    {
      this.DefaultStyleKey = typeof( DataRow );
      this.UpdateInitializationState( false );
    }


    #region AlternatingBackground Property

    public static readonly DependencyProperty AlternatingBackgroundProperty = DependencyProperty.Register(
      "AlternatingBackground",
      typeof( Brush ),
      typeof( DataRow ),
      new PropertyMetadata( null ) );

    public Brush AlternatingBackground
    {
      get
      {
        return ( Brush )this.GetValue( DataRow.AlternatingBackgroundProperty );
      }
      set
      {
        this.SetValue( DataRow.AlternatingBackgroundProperty, value );
      }
    }

    #endregion // AlternatingBackground Property

    internal bool IsDisplayInitialized
    {
      get
      {
        return m_isInitialized;
      }
      set
      {
        if( value != m_isInitialized )
        {
          m_isInitialized = value;
          this.UpdateInitializationState( true );
        }
      }
    }

    #region IsAlternate Property

    internal bool IsAlternate
    {
      get;
      set;
    }

    #endregion //Alternate Property

    #region IsSelected property
    internal bool IsSelected
    {
      get
      {
        return m_isSelected;
      }
      set
      {
        if( value != m_isSelected )
        {
          m_isSelected = value;
        }
      }
    }
    #endregion

    internal override void Accept( IVisitor visitor )
    {
      visitor.Visit( this );
    }

    internal void SetBackground( Brush rowBackground )
    {
      if( rowBackground == null )
        this.ClearValue( Row.BackgroundProperty );
      else
        this.Background = rowBackground;
    }

    internal void SetAlternatingBackground( Brush rowAlternatingBackground )
    {
      if( rowAlternatingBackground == null )
        this.ClearValue( DataRow.AlternatingBackgroundProperty );
      else
        this.AlternatingBackground = rowAlternatingBackground;
    }

    internal void SetCommonState( string state, bool useTransitions )
    {
      this.SetRowCommonStateOnly( state, useTransitions );
    }

    internal void SetRowCommonStateOnly( string state, bool useTransitions )
    {
      this.VisualStateHelper.GoToCommonState( state, useTransitions );
    }

    internal override void AddColumnElement( ColumnElementWrapper element )
    {
      base.AddColumnElement( element );
      ( ( DataCellWrapper )element ).NotifyRowDisplayInitialized();
    }

    internal void UpdateInitializationState( bool useTransitions )
    {
      string state = ( this.IsDisplayInitialized )
        ? DataRow.InitializationStateInitialized
        : DataRow.InitializationStateNotInitialized;
      this.VisualStateHelper.GoToInitializationState( state, useTransitions );
    }

    private bool m_isSelected;
    private bool m_isInitialized;
  }
}
